package de.tyde.mylazysms;

import java.io.IOException;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class MySMSReceiver extends BroadcastReceiver {

	private static final String LOG_TAG = "SMSReceiver";
	private DBhelper dbConnection = null;
	MyAccount user = null;
	AccountManager accountManager = null;
	String absender;
	String smsText;
	String authToken;

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(LOG_TAG, "------------------------------------sms received---------------------------------");
		
		accountManager = AccountManager.get(context);
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		dbConnection = new DBhelper(context);
		dbConnection.open();
		Cursor c = dbConnection.getUser();
		if (c.getCount() > 0) {
			c.moveToFirst();
			Account[] accounts = accountManager.getAccountsByType("com.google");
			for (int x = 0; x < accounts.length; x++) {
				if (accounts[x].name.equals(c.getString(c
						.getColumnIndex("user")))) {
					user = new MyAccount(accounts[x]);
					user.pw = c.getString(c.getColumnIndex("pw"));
					Log.i(LOG_TAG, "User found");
				}
			}
		}

		if (bundle != null) {
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				absender = msgs[i].getOriginatingAddress();
				smsText = msgs[i].getMessageBody().toString();
			}
		}
		Log.i(LOG_TAG,"Start sending Mail");
		Log.i(LOG_TAG,"User: " + user.account.name);
		
		Mail m = new Mail(user.account.name, user.pw);
		String[] toArr = { user.account.name };
		m.set_from(user.account.name);
		m.set_subject("Sie haben eine neue SMS von " + absender + " erhalten");

		m.setBody(smsText
				+ "\n\ngehen sie auf mylazysms.appspot.com um auf diese sms zu antworten");
		m.set_to(toArr);
		try {
			m.send();
			Log.i(LOG_TAG, "Mail sended");
		} catch (Exception e) {
			e.printStackTrace();
		}

		// try {
		// HttpClient client = new DefaultHttpClient();
		// String postURL = "http://mylazysms.appspot.com";
		// HttpPost post = new HttpPost(postURL);
		// List<NameValuePair> params = new ArrayList<NameValuePair>();
		// String user ="";
		// dbConnection = new DBhelper(context);
		// dbConnection.open();
		// Cursor c = dbConnection.fetchEntry();
		// if(c.getCount() > 0){
		// c.moveToFirst();
		// user = c.getString(c.getColumnIndex("user"));
		// }
		//
		// params.add(new BasicNameValuePair("isApp", "true"));
		// params.add(new BasicNameValuePair("user", user));
		// if(bundle != null){
		// Object[]pdus = (Object[]) bundle.get("pdus");
		// msgs = new SmsMessage[pdus.length];
		// for(int i = 0; i < msgs.length; i++){
		// msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
		// params.add(new BasicNameValuePair("SMSabsender",
		// msgs[i].getOriginatingAddress()));
		// params.add(new BasicNameValuePair("SMStext",
		// msgs[i].getMessageBody().toString()));
		// }
		// }
		// UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,
		// HTTP.UTF_8);
		// post.setEntity(ent);
		// HttpResponse responsePOST = client.execute(post);
		//
		// HttpEntity resEntity = responsePOST.getEntity();
		// if (resEntity != null) {
		// Log.i("RESPONSE", EntityUtils.toString(resEntity));
		// System.out.println("RESPONSE"+ EntityUtils.toString(resEntity));
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

	

}
